/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.action;

import java.util.ArrayList;
import java.util.Iterator;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.PerforceException;
import net.sourceforge.perforce.core.internal.api.ChangeSpec;
import net.sourceforge.perforce.core.resources.IChangelist;
import net.sourceforge.perforce.ui.dialogs.ChangeSpecDialog;
import net.sourceforge.perforce.ui.dialogs.DescribeChangelistDialog;
import net.sourceforge.perforce.ui.internal.Policy;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.action.IAction;

/**
  * @version $Revision: 196 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class EditChangelistSpecAction extends PerforceAction 
{
	// ------------------------------------------------------------------------------ Public Methods

	/** @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) */
	public void run(IAction action) 
	{
		IChangelist[] changelists = getSelectedChangelists();
		Policy.assertTrue(changelists.length == 1);
		
		IChangelist changelist = changelists[0];
		boolean readOnly = !changelist.getClientName().equals(changelist.getServer().getClient());
		try
		{
			if (!readOnly)
			{
				String buttonText =	Policy.bind("ChangeSpecDialog.okButtonTextUpdate");//$NON-NLS-1$
				ChangeSpecDialog dialog = 
					ChangeSpecDialog.newInstance(getShell(), changelist, buttonText);
				
				if (dialog.open() == ChangeSpecDialog.OK && !readOnly)
				{
					ChangeSpec changeSpec = dialog.getChangeSpec();
					changelist.getServer().change(changeSpec);
					
					PerforceCorePlugin.getPlugin().broadcastChangelistStateChanges(
						new IChangelist[] { changelist });
				}
			}
			else
			{
				DescribeChangelistDialog dialog = 
					new DescribeChangelistDialog(getShell(), changelist);
				dialog.open();
			}
		}
		catch (PerforceException ex)
		{
			handle(ex);
		}
	}

	// --------------------------------------------------------------------------- Protected Methods

	/** @see net.sourceforge.perforce.ui.action.PerforceAction#isEnabled() */
	protected boolean isEnabled() 
		throws PerforceException 
	{
		IChangelist[] changelists = getSelectedChangelists();
		if (changelists.length != 1)
		{
			return false;
		}
		
		IChangelist changelist = changelists[0];
		if (changelist.getId() == IChangelist.DEFAULT)
		{
			return false;
		}
		
		return true;
	}
	
	// ----------------------------------------------------------------------------- Private Methods
	
	/** Called to retrieve an array of selected changelists. 
	  * 
	  * @return array of selected changelists.
	  */
	protected IChangelist[] getSelectedChangelists()
	{
		ArrayList resources = null;
		if (!getSelection().isEmpty()) 
		{
			resources = new ArrayList();
			for (Iterator elements = getSelection().iterator(); elements.hasNext();)
			{
				Object next = elements.next();
				if (next instanceof IChangelist) 
				{
					resources.add(next);
					continue;
				}
				else
				if (next instanceof IAdaptable) 
				{
					IAdaptable a = (IAdaptable) next;
					Object adapter = a.getAdapter(IChangelist.class);
					if (adapter instanceof IChangelist) 
					{
						resources.add(adapter);
						continue;
					}
				}
			}
		}
		
		if (resources != null && !resources.isEmpty()) 
		{
			return (IChangelist[]) resources.toArray(new IChangelist[resources.size()]);
		}
		
		return new IChangelist[0];
	}
}
